﻿using System;
using System.Text;

// Write a program that reads two dates in the format: day.month.year and calculates the number of days between them.

namespace DateDistance
{
    class DateDistance
    {
        static DateTime ParseDate(string input)
        {
            StringBuilder number = new StringBuilder();
            //[0] = Day, [1] = Month, [2] = Year
            int[] date = new int[3];
            int dateIndex = 0;

            //Search Year, mounth and day in the string
            for (int charIndex = 0, length = input.Length; charIndex < length; charIndex++)
            {
                if (char.IsDigit(input[charIndex]))
                {
                    number.Append(input[charIndex]);
                }
                else
                {
                    if (number.Length > 0)
                    {
                        date[dateIndex++] = int.Parse(number.ToString());
                        number.Clear();
                    }
                }
            }

            if (number.Length > 0)
            {
                date[dateIndex] = int.Parse(number.ToString());
            }

            return new DateTime(date[2], date[1], date[0]);
        }

        static int Distance(DateTime startDate, DateTime endDate)
        {
            int days = 0;

            if (startDate < endDate)
            {
                for (DateTime currentDate = endDate; currentDate > startDate; currentDate = currentDate.AddDays(-1))
                {
                    days++;
                }
            }
            else
            {
                for (DateTime currentDate = endDate; currentDate < startDate; currentDate = currentDate.AddDays(1))
                {
                    days++;
                }
            }

            return days;
        }

        static void Main()
        {
            Console.Write("Enter the first date: ");
            string firstDate = Console.ReadLine();
            Console.Write("Enter the second date: ");
            string secondDate = Console.ReadLine();

            DateTime startDate = ParseDate(firstDate);
            DateTime endDate = ParseDate(secondDate);

            Console.WriteLine("Distance: {0} days.", Distance(startDate, endDate));
        }
    }
}
